package com.zhdl.modules.demo.DCHTProcess.camera;

import cn.hutool.core.util.HexUtil;
import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.BaseConversionTools;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.hardware_contact.hardware_common.param.CurrencyProtocolWriteReq;
import com.zhdl.hardware_contact.hardware_common.param.TcpAssembleCmdPkg;
import com.zhdl.modules.demo.DCHTProcess.CHTRobotInterface;
import com.zhdl.modules.demo.DCHTProcess.CHTSmallRobotInterface;
import com.zhdl.modules.demo.DCHTProcess.RobotTestProcess;
import com.zhdl.modules.process.common.AfterCurrentProcessDo;
import com.zhdl.modules.process.dto.ProcessBean;
import com.zhdl.modules.process.task.CustomizeTask;
import com.zhdl.modules.process.task.DynamicTask;
import com.zhdl.modules.process.verify.PlCBitValueVerify;
import com.zhdl.network.strategy.common_client.impl.CommonProtocolStrategy;
import com.zhdl.network.strategy.common_server.impl.CommonServerSendServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

import static com.zhdl.modules.demo.DCHTProcess.camera.CameraInterface.*;

@Slf4j
@Service
public class CameraInitService {

    @Resource
    DynamicTask dynamicTask;

    @Resource
    CameraInterface cameraInterface;

    @Resource
    CommonServerSendServiceImpl commonServerSendService;
    @Resource
    RobotTestProcess robotTestProcess;

    @Resource
    CommonProtocolStrategy commonProtocolStrategy;
    /**
     * 相机标定动作
     */
    public String calibAction(int location) {
        // 获取当前标定步数的流程
        float x = cameraInterface.robotCoordinate[location][0];
        float y = cameraInterface.robotCoordinate[location][1];
        float r = cameraInterface.robotCoordinate[location][2];
        String coordinate = x + "," + y + "," + r;
        log.info("当前标定点位"+location+"左边==="+coordinate);
        CurrencyProtocolWriteReq currencyProtocolReadReq1 = new CurrencyProtocolWriteReq();
        currencyProtocolReadReq1.setId("01");
        currencyProtocolReadReq1.setStartAddress("0119");
        String locationStr = BaseConversionTools.floatTo8hexHL(x) + BaseConversionTools.floatTo8hexHL(y) + BaseConversionTools.floatTo8hexHL(r);
        currencyProtocolReadReq1.setWriteValue(locationStr);

        try {
            commonProtocolStrategy.sendCmd(CHTRobotInterface.ROBOT_IP, HexUtil.encodeHexStr(TcpAssembleCmdPkg.write10(currencyProtocolReadReq1)), s -> {

            }, 2000);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        if (location <=11 ){
            ProcessBean processByStep = robotTestProcess.getProcessByStep(17.0f, 3);
            processByStep.addAfterProcess(lastProcess -> {
//                ProcessBean processBean = new ProcessBean("相机标定动作-" + location, 0)
//                        .addDoSomeThing(() -> {
//                            //发送标定坐标
//                            commonServerSendService.sendToClientCmd(CAMERA_SERVER_IP, CAMERA_CLIENT_IP, "Cailb," + location + "," + coordinate);
//                        })
//                        .addProcessVerify(new PlCBitValueVerify(cameraInterface, CAMERA_CALIB_RESULT + location))
//                        .addAfterProcess(lastProcess1 -> {
////                    log.info("相机标定,流程数->{}", lastProcess);
////                    calibAction(lastProcess);
//                        });
//                //执行当前任务
//                processBean.getProcessDo().doSomeThing();
//                dynamicTask.startTask(new CustomizeTask(processBean), 100);

           //     DelayUtil.delayedExecution(3000, () -> calibAction(location+1));

            });
            processByStep.getProcessDo().doSomeThing();
            dynamicTask.startTask(new CustomizeTask(processByStep),100);
            DelayUtil.delayedExecution(1000, () -> {
                CurrencyProtocolWriteReq currencyProtocolReadReq = new CurrencyProtocolWriteReq();
                currencyProtocolReadReq.setId("01");
                currencyProtocolReadReq.setStartAddress("010F");
                currencyProtocolReadReq.setWriteValue("00000000");
                try {
                    commonProtocolStrategy.sendCmd(CHTRobotInterface.ROBOT_IP, HexUtil.encodeHexStr(TcpAssembleCmdPkg.write10(currencyProtocolReadReq)), s -> {

                    }, 2000);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });

        }else{
            dynamicTask.startTask(new CustomizeTask( robotTestProcess.getProcessByStep(18.0f, 3)),100);
        }

        //需要执行的任务

        return coordinate;
    }



    private void startCoordinate(){


    }

    /**
     * 相机拍照的动作
     */
    public void takePhotoAction(int type, int number, CommonCallback<String> callback) {
        //规定 当前需要拍照的相机 以及 需要拍照的步骤
        AtomicInteger ident = type == 1 ? new AtomicInteger(101) : new AtomicInteger(201);
        AtomicInteger identNumber = number == 1 ? new AtomicInteger(0) : new AtomicInteger(1);

        //规定需要验证的步骤
        String verification;

        if (type == 1) {
            verification = number == 1 ? CAMERA1_TAKE_PHOTO1 : CAMERA1_TAKE_PHOTO2;
        } else {
            verification = number == 1 ? CAMERA2_TAKE_PHOTO1 : CAMERA2_TAKE_PHOTO2;
        }

        ProcessBean processBean = new ProcessBean("通知相机程序拍照", 0)
                .addDoSomeThing(() -> {
                    //发送标定坐标
                    String hexCmd = BaseConversionTools.asciiToHex("T" + (ident.get() + identNumber.get()) + ",1");

                    commonServerSendService.sendToClientCmd(CAMERA_SERVER_IP, CAMERA_CLIENT_IP,hexCmd);
                })
                .addProcessVerify(new PlCBitValueVerify(cameraInterface, verification))
                .addAfterProcess(lastProcess -> {
                    if (type == 1){
                        if (number == 1){
                            callback.statusCallback(cameraInterface.getValue(CAMERA1_PHOTO_RESULT).value);
                        }else{
                            callback.statusCallback(cameraInterface.getValue(CAMERA1_TAKE_PHOTO2).value + "_" +cameraInterface.getValue(CAMERA1_PHOTO_RESULT).value);

                        }
                    }else if (type == 2){
                        if (number == 1){
                            callback.statusCallback(cameraInterface.getValue(CAMERA2_TAKE_PHOTO1).value);
                        }else{
                            callback.statusCallback(cameraInterface.getValue(CAMERA2_TAKE_PHOTO2).value +"_"+ cameraInterface.getValue(CAMERA2_PHOTO_RESULT).value);
                        }
                    }
                });
        //执行当前任务
        processBean.getProcessDo().doSomeThing();
        dynamicTask.startTask(new CustomizeTask(processBean), 100);
    }
}
